<?php
class EmptyAction extends CommonAction {
	public $categoryModel;
	public $newsModel;
	public $pagenav;
	public $template;
	public function _initialize(){
		parent::_initialize(); 
		$this->categoryModel = D('Category');
		$this->newsModel=D('News');
		$ModulesDB = D('Modules');
		$cate=$this->categoryModel->where(array('name'=> array('like',strtolower(MODULE_NAME)."%"),'site_id'=>__SITEID__))->find();
		
		
		if(strpos($cate['name'], '.txt') > 0){
			exit($cate['title']);
		}
		
		if($this->_get('displayall') > 0){
			setcookie('displayall_'.__SITEID__, 1, 3600+time(), '/');
		}
		//echo $this->categoryModel->getLastSql();exit;
		if(!is_array($cate) && !$this->Product()){
			$this->_empty();
		}
		if(!array($cate)){
			
			$this->_empty();
		}else{
			if($cate['permission']>=0){
				if(!session('memberInfo')){
					$this->error("此内容必须登录才能查看",U('Member/Public/login'));
				}else{
					if(intval($this->memberInfo['socre']) < intval($cate['permission'])){
					    $this->error("您积分不够,无法查看此内容");
				    }
				}
			}
		}
		if(!empty($this->curcate['out_link'])){
			header("location:".$this->curcate['out_link']);
		}
		//页面导航数组 
		$this->pagenav=$this->getPageNav($cate['id']);
		$this->template=$this->getCurModelName();
		$this->assign('pagenav',$this->pagenav);
		
		$CategoryList =$this->categoryModel -> where(array('site_id' => __SITEID__, 'pid' => $this->pagenav[0]['id'])) -> order('sort')-> select();
		$this->assign('CategoryList', $CategoryList);
		//栏目轮播图
		
		foreach($this->pagenav as $v){
			if(count(unserialize($v['banner']))>0){
				$this->assign('banner',unserialize($v['banner']));
			}
		}
		
		
		//dump(unserialize($cate['banner']));
		$this->curcate=$cate;
		//操作名是否为数字,由此判断页面是否为列表
		
		$subCategory = $this->categoryModel -> where(array('pid' => $cate['id'],'site_id' => __SITEID__)) -> order('sort') -> select();
		if(!$subCategory){
			$subCategory = $this->categoryModel -> where(array('pid' => $cate['pid'],'site_id' => __SITEID__)) -> order('sort') -> select();
		}
		$this -> assign('subCategory', $subCategory);
		
		
		
		if(intval(ACTION_NAME)==0){
			//产生分页变量
			//dump(C('VAR_PAGE'));
			//dump(strpos(ACTION_NAME,C('VAR_PAGE')));
			if(0===strpos(ACTION_NAME,C('VAR_PAGE'))){
				$p=explode('-',ACTION_NAME);
				$_GET[C('VAR_PAGE')]=$p[1];
				$_REQUEST[C('VAR_PAGE')]=$p[1];
			}
			$ids=$this->categoryModel->getAllIDBypid($cate['id'], __SITEID__);
			$map['n.category_id']=array('in',$ids);
			$map['n.status']=1;
			$map['n.site_id']=__SITEID__;
			
			if($this->_get('keywords')){
			$map['n.title'] = array('like', '%'.$this->_get('keywords').'%');
		}
			
			
			//SEO
			$seo['title']=empty($cate['page_title'])?$cate['title']:$cate['page_title'];
			$seo['keywords']=$cate['keywords'];
			$seo['description']=$cate['description'];
			$this->assign('seo',$seo); 
			$this->img_url = $cate['img_url'];
			$this->initView();
			
			$pagesize = $this->getPagesize();
			if($pagesize<=0){
				$pagesize = 15;
			}
			$this->newsModel->_list($this->view,$map,'n.sort,n.datetime desc',intval($pagesize));
			$map['n.category_id']= $cate['id'];
			$this->newsModel->_listSelf($this->view,$map,'n.sort,n.datetime desc',intval($pagesize));
			//echo $this->newsModel -> getLastSql();
			$list = $this->view->get('list');
			foreach($list as $k=>$v){
				$list[$k]['extends'] = getNewsExtends($v['id']);
			}
			//if(isset($_GET['dump'])){dump($list);}
			 



			/*if(__SITEID__ == 379){
				dump($this->subCategory);
			}*/
			
			
			$this->assign('list',$list); 
			if(isset($_GET['xx'])){
				dump($this->template);
			}
			
			$this->getModulesByID($this->view, $ModulesDB);
			$request = $this->view -> fetch('Public:header');
			$request .= $ModulesDB->getListToString($this->template['page_list_header_id'],$this->view);
			$request .= $ModulesDB->getListToString($this->template['page_list_id'],$this->view);
			$request .= $ModulesDB->getListToString($this->template['page_list_footer_id'],$this->view);
			$request .= $this->view -> fetch('Public:footer');
			$request .= $this->view -> fetch('Public:footerscript');
			if(isset($_GET['isajaxlist'])){
				exit(json_encode(array('empty' => 0,'tbody'=>$request,'dump'=>$_REQUEST,'statu'=>1)));
			}
			exit($request);
			
		}else{ 
			$id=intval(ACTION_NAME);
			$data=$this->newsModel->where(array('id'=>$id,'status'=>1,'site_id'=>__SITEID__))->find();
			if(!array($data)){
				$this->error('页面不存在');
			}else{
				if($data['permission']>=0){
					if(!session('memberInfo')){
						$this->error("此内容必须登录才能查看",U('Member/Public/login'));
					}else{
						if(intval($this->memberInfo['socre']) < intval($data['permission'])){
							$this->error("您积分不够,无法查看此内容");
						}
					}
				}
			}
			
			
			$this->newsModel->where(array('id'=>$id,'status'=>1,'site_id'=>__SITEID__))->data(array('click'=>$data['click']+1))->save();
			//上一个
			$this->prev=$this->newsModel->where('id<'.$id.' and status=1 and category_id='.$cate['id'])->find();
			//下一个
			$this->next=$this->newsModel->where('id>'.$id.' and status=1 and category_id='.$cate['id'])->find();
			
			//SEO
			$seo['title']=empty($data['page_title'])?$data['title']:$data['page_title'];
			$seo['keywords']=$data['keywords'];
			$seo['description']=$data['description'];
			
			
			$this->assign('seo',$seo);
			if(!empty($data['mimg_url'])){
				$this->img_list=explode(',',$data['mimg_url']);
			}
			
			$imgList = array();
			foreach($this->img_list as $k => $v){
				$imgList[$k]['title'] = $data['title']; 
				$imgList[$k]['img_url'] = $v;
			}
			
			$this -> assign('imgList', $imgList);
			
			
			//$extend=M("News_extends")->where(array('news_id'=>$data['id']))->select();
			$this->assign('extend',getNewsExtends($data['id']));
			
			
			if(isset($_GET['dump'])){dump($this->get('extend'));}
			$this->initView();
			if($data['modules_id'] > 0){
				$data['content'] .= $ModulesDB -> getModulesSource($data['modules_id'], $this->view);
			}
			
			$this->assign($data);
			$this->assign('relate',unserialize($data['relate']));
			if($data['outlink'] != ''){
				header("location:".$data['outlink']);
			}
			//存在会员推荐
			
			
			if(isset($_COOKIE['affid']) && intval($_COOKIE['affid']) > 0){
			    $mdb = M('Member');
				$this->aff = $mdb->where(array('id' => $_COOKIE['affid']))->find();
			}
			//读取模块
			$this->initView();
			$this->getModulesByID($this->view, $ModulesDB);
			$request = $this->view -> fetch('Public:header');
			$request .= $ModulesDB->getListToString($this->template['page_view_header_id'],$this->view);
			$request .= $ModulesDB->getListToString($this->template['page_view_id'],$this->view);
			$request .= $ModulesDB->getListToString($this->template['page_view_footer_id'],$this->view);
			$request .= $this->view -> fetch('Public:footer');
			$request .= $this->view -> fetch('Public:footerscript');
			if(isset($_GET['isajaxlist'])){
				exit(json_encode(array('empty' => 0,'tbody'=>$request,'dump'=>$_REQUEST,'statu'=>1)));
			}
			exit($request);
		}
		exit();
	}
	//不存在这个页面时
    public function _empty(){
		//dump($_REQUEST);
		ob_end_clean();
		header('HTTP/1.1 404 Not Found'); 
        header("status: 404 Not Found");exit();
		//echo "该页面不存在";
    }
	//获取当前模板文件名
	public function getCurModelName(){
		$template=array();
		foreach($this->pagenav as $k=>$v){
			if($v['page_list_id']!=0){
				$template['page_list_id']=$v['page_list_id'];
			}
			if($v['page_list_header_id']!=0){
				$template['page_list_header_id']=$v['page_list_header_id'];
			}
			if($v['page_list_footer_id']!=0){
				$template['page_list_footer_id']=$v['page_list_footer_id'];
			}
			if($v['page_view_id']!=0){
				$template['page_view_id']=$v['page_view_id'];
			}
			if($v['page_view_header_id']!=0){
				$template['page_view_header_id']=$v['page_view_header_id'];
			}
			if($v['page_view_footer_id']!=0){
				$template['page_view_footer_id']=$v['page_view_footer_id'];
			}
		}
		return $template;
	}
	
	public function getPagesize(){
		$pagesize = 0;
		foreach($this->pagenav as $k=>$v){
			if($v['pagesize']>0){
				$pagesize=$v['pagesize'];
			}
		}
		return $pagesize;
	}
	
	//获取页面导航
	public function getPageNav($pid=0){
		$arr=$this->getPageNavArr($pid);
		
		return array_reverse($arr);
		return $arr;
	}
	
	//获取页面导航数组
	public function getPageNavArr($pid=0,$arr=array()){
		$re=$this->categoryModel->where(array('id'=>$pid,'site_id'=>__SITEID__))->find();
		if(!is_array($re)){
			return $arr;
		}else{
			$arr[]=$re;
			return $this->getPageNavArr($re['pid'],$arr);
		}
	}
	
	//获取所有子类ID包含自身
	public function getAllSubCate_id($cate,$isIncludeSelf=true){
		$ids=$this->getAllSubCate($cate['id']);
		
		if($isIncludeSelf){
			$ids[]=$cate;
		}
		foreach($ids as $k=>$v){
			$str[]=$v['id'];
		}
		return implode(',',$str);
	}
	//获取所有子类
	public function getAllSubCate($id=0,$arr){
		return $this->categoryModel->getListToArray1($id,__SITEID__);
	}
	
	
	public function Product(){
		$categoryModel = D('Product_category');
		$cate=$categoryModel->where(array('name'=> array('like',strtolower(MODULE_NAME)."%"),'site_id'=>__SITEID__))->find();
//		dump($cate);exit;
		//echo $this->categoryModel->getLastSql();exit;
		if(!is_array($cate)){
			return false;
		}

		//操作名是否为数字,由此判断页面是否为列表
		if(intval(ACTION_NAME)==0){
//			var_dump(1);exit;
			$Action = A('Home/Product_category');
			$Action -> listorder = 'p.sort';
			$_GET['id'] = $cate['id'];
			$Action -> index();
		    //echo '列表';
			exit;
		}else{
			$Action = A('Home/Product');
			$_GET['id'] = intval(ACTION_NAME);
			$Action -> index();
		    //echo '列表';
			exit;
		}
		return true;
	}
}
?>